// @Author chenyong 2023/2/27 12:27:00
package mindMap

import (
	"gitee.com/gychenyong/go-case/model"
	"gitee.com/gychenyong/go-case/utils"
)

var caseInfo model.CaseInfo

type RootTopic struct {
	Id       string `json:"id"`
	Title    string `json:"title"`
	Children struct {
		Attached []Topic `json:"attached"`
	} `json:"children"`
}

type Topic struct {
	Id       string `json:"id"`
	Title    string `json:"title"`
	Children struct {
		Attached []Topic `json:"attached"`
	} `json:"children"`
	Markers []Marker `json:"markers"`
}

type Marker struct {
	MarkerId string `json:"markerId"`
}

func (r *RootTopic) MindToCase(outputFile string) {
	if len(r.Children.Attached) != 0 {
		for _, topic := range r.Children.Attached {
			r.SetModel(topic)
		}
		utils.WriteCsv(caseInfo.GetCases(), outputFile)
	}
}

// @author:chenyong
// @function:SetModel
// @Description: 模块名
// @receiver r
// @param modelTopic
func (r *RootTopic) SetModel(modelTopic Topic) {

	caseInfo.Model = modelTopic.Title // 设置模块名称

	if len(modelTopic.Children.Attached) != 0 {
		for _, topic := range modelTopic.Children.Attached {
			r.SetFunctionPoint(topic)
		}
	}
}

// @author:chenyong
// @function:SetFunctionPoint
// @Description: 功能点
// @receiver r
// @param functionPointTopic
func (r *RootTopic) SetFunctionPoint(functionPointTopic Topic) {
	caseInfo.FunctionPoint = functionPointTopic.Title
	if len(functionPointTopic.Children.Attached) != 0 {
		for _, topic := range functionPointTopic.Children.Attached {
			caseInfo.Stage = ""
			caseInfo.Type = ""
			caseInfo.Precondition = ""
			r.CheckTitle(topic)
		}
	}
}

// @author:chenyong
// @function:CheckTitle
// @Description: 判断主题是不是用例名称
// @receiver r
// @param topic
func (r *RootTopic) CheckTitle(topic Topic) {
	if len(topic.Title) >= 13 {
		if topic.Title[:12] == "前置条件" {
			for _, child := range topic.Children.Attached {
				caseInfo.Precondition = topic.Title[15:]
				r.CheckTitle(child)
			}
		} else if topic.Title[:12] == "用例类型" {
			for _, child := range topic.Children.Attached {
				caseInfo.Type = topic.Title[15:]
				r.CheckTitle(child)
			}
		} else if topic.Title[:12] == "适用阶段" {
			for _, child := range topic.Children.Attached {
				caseInfo.Stage = topic.Title[15:]
				r.CheckTitle(child)
			}
		} else {
			r.SetTitle(topic)
		}
	} else {
		r.SetTitle(topic)
	}
}

// @author:chenyong
// @function:SetTitle
// @Description: 用例名称
// @receiver r
// @param titleTopic
func (r *RootTopic) SetTitle(titleTopic Topic) {
	caseInfo.Title = titleTopic.Title
	// 获取优先级
	if len(titleTopic.Markers) != 0 {
		priority := titleTopic.Markers[0].MarkerId
		caseInfo.Priority = priority[len(priority)-1:]
	} else {
		caseInfo.Priority = "3"
	}
	if len(titleTopic.Children.Attached) != 0 {
		r.CheckStep(titleTopic)
	}
}

// @author:chenyong
// @function:CheckStep
// @Description: 判断主题是不是步骤或分组
// @receiver r
// @param topic
func (r *RootTopic) CheckStep(topic Topic) {
	var status = true
	for _, child := range topic.Children.Attached {
		if len(child.Title) >= 13 {
			if child.Title[:12] == "前置条件" {
				if caseInfo.Precondition == "" {
					caseInfo.Precondition = child.Title[15:]
				} else {
					caseInfo.Precondition = caseInfo.Precondition + "\n" + child.Title[15:]
				}
				if len(child.Children.Attached) != 0 {
					r.CheckStep(child)
				}
				status = false
			} else if child.Title[:12] == "用例类型" {
				caseInfo.Type = child.Title[15:]
				if len(child.Children.Attached) != 0 {
					r.CheckStep(child)
				}
				status = false
			} else if child.Title[:12] == "适用阶段" {
				if caseInfo.Precondition == "" {
					caseInfo.Stage = child.Title[15:]
				} else {
					caseInfo.Stage = caseInfo.Stage + "\n" + child.Title[15:]
				}
				if len(child.Children.Attached) != 0 {
					r.CheckStep(child)
				}
				status = false
			}
		}
	}
	if status {
		caseInfo.StepGroups = nil
		caseInfo.ExpectationGroups = nil
		if len(topic.Children.Attached) == 1 {
			stepGroup := model.StepGroup{
				GroupName: "",
				StepName:  nil,
			}
			stepGroup.StepName = append(stepGroup.StepName, topic.Children.Attached[0].Title)
			expectationGroup := model.ExpectationGroup{
				ExpectationName: nil,
			}
			r.SetStep(topic.Children.Attached[0], stepGroup, expectationGroup)
		} else {
			for _, group := range topic.Children.Attached {
				stepGroup := model.StepGroup{
					GroupName: "",
					StepName:  nil,
				}
				stepGroup.GroupName = group.Title // 分组名
				expectationGroup := model.ExpectationGroup{
					ExpectationName: nil,
				}
				r.SetStep(group, stepGroup, expectationGroup)
			}
		}
		caseInfo.Save()
	}
}

func (r *RootTopic) SetStep(topic Topic, stepGroup model.StepGroup, expectationGroup model.ExpectationGroup) {
	if len(topic.Children.Attached) == 2 {
		if len(stepGroup.StepName) != 0 { // 分组名不解析预期结果
			expectationGroup.ExpectationName = append(expectationGroup.ExpectationName, topic.Children.Attached[0].Title)
		}
		stepGroup.StepName = append(stepGroup.StepName, topic.Children.Attached[1].Title)
		r.SetStep(topic.Children.Attached[1], stepGroup, expectationGroup)
	} else if len(topic.Children.Attached) == 1 {
		if len(topic.Children.Attached[0].Children.Attached) != 0 {
			if len(stepGroup.StepName) != 0 { // 分组名不解析预期结果
				expectationGroup.ExpectationName = append(expectationGroup.ExpectationName, "")
			}
			stepGroup.StepName = append(stepGroup.StepName, topic.Children.Attached[0].Title)
		}
		r.SetStep(topic.Children.Attached[0], stepGroup, expectationGroup)
	} else if len(topic.Children.Attached) == 0 {
		expectationGroup.ExpectationName = append(expectationGroup.ExpectationName, topic.Title)
		caseInfo.StepGroups = append(caseInfo.StepGroups, stepGroup)
		caseInfo.ExpectationGroups = append(caseInfo.ExpectationGroups, expectationGroup)
	}
}
